
/*ajimber */
package vista.ListaInteres;

import java.util.ArrayList;
import java.util.List;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import modelo.entidades.ListaInteres;
import Utilidades.Fechas;

public class ListaInteresTableModel implements TableModel {

    private List<ListaInteres> listaInteres = new ArrayList<ListaInteres>();
    private List<TableModelListener> tableListeners = new ArrayList<TableModelListener>();
    //private List<ListDataListener> dataListeners = new ArrayList<ListDataListener>();
    private static ListaInteresTableModel model = null;

    private ListaInteresTableModel() {
    }

    public static ListaInteresTableModel create() {
        if (model == null) {
            model = new ListaInteresTableModel();
        }
        return model;
    }

    public List<ListaInteres> getListaInteres() {
        return listaInteres;
    }

    public void setListaInteres(List<ListaInteres> listaInteres) {
        this.listaInteres = listaInteres;
        fireContentsChanged();
    }

    protected void fireContentsChanged() {
        fireContentsChangedTableModel();
    }
    //--------------- MÉTODOS PROPIOS DE TableModel -------------------------

    public ListaInteres getRow(int indexRow) {
        return listaInteres.get(indexRow);
    }

    public int getRowCount() {
        return listaInteres.size();
    }

    public int getColumnCount() {
        return 3;
    }

    public String getColumnName(int columnIndex) {
        String name = null;
        switch (columnIndex) {
            case 0:
                name = "Matricula";
                break;
            case 1:
                name = "Fecha";
                break;
            case 2:
                name = "Motivo";
                break;

        }
        return name;
    }

    public Class<?> getColumnClass(int columnIndex) {
        return String.class; //Todos
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false; //No permito editar celdas
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        ListaInteres lista = listaInteres.get(rowIndex);
        String value = null;
        switch (columnIndex) {
            case 0:
                value = lista.getMatricula();
                break;
            case 1:
               value = Fechas.fechaATexto(lista.getFecha());
                break;
            case 2:
                value = lista.getMotivo();
                break;
        }
        return value;

    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void addTableModelListener(TableModelListener l) {
        tableListeners.add(l);
    }

    public void removeTableModelListener(TableModelListener l) {
        tableListeners.remove(l);
    }

    protected void fireContentsChangedTableModel() {
        //TableModelEvent event = new TableModelEvent(this,0,getRowCount());
        TableModelEvent event = new TableModelEvent(this, 0, this.getRowCount() - 1, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT);
        for (TableModelListener listener : tableListeners) {
            listener.tableChanged(event);
        }
    }
}
